<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./jssip-3.3.6.js"></script>
    <script src="./jquery.min.js"></script>
</head>
<body>
<video id="remoteVideo"></video>
<video id="localVideo" muted="muted"></video>
<div>状态：<span id="status">注册中...</span></div>
<div>
    <input type="text" id="phone">
    <button type="button" onclick="testCall()">拨打</button>
    <button type="button" onclick="hangup()">挂断</button>
    <button type="button" id="accept">接听</button>
</div>
<script>
    JsSIP.debug.disable('JsSIP:*');
    JsSIP.C.SESSION_EXPIRES=120;
    JsSIP.C.MIN_SESSION_EXPIRES=120;

    var remoteVideo = document.getElementById('remoteVideo');

    var localStream = null;
    var outgoingSession = null;
    var incomingSession = null;
    var currentSession = null;
    var wss_url = 'wss://ssl.nicaicai.top:7443';

    var socket = new JsSIP.WebSocketInterface(wss_url);
    var configuration = {
        sockets: [socket],
        outbound_proxy_set: wss_url,
        uri: "sip:1001@ssl.nicaicai.top",
        password: 1234,
        register: true,
        session_timers: false,
        audio: true
    };

    userAgent = new JsSIP.UA(configuration);

    //注册成功
    userAgent.on('registered', function (data) {
        $("#status").text("注册成功");
        console.info("registered: ", data.response.status_code, ",", data.response.reason_phrase);
    });

    //注册失败
    userAgent.on('registrationFailed', function (data) {
        $("#status").text("注册失败");
        console.warn("registrationFailed, ", data.response.status_code, ",", data.response.reason_phrase, " cause - ", data.cause);
    });

    //注册超时
    userAgent.on('registrationExpiring', function () {
        $("#status").text("注册超时");
        console.warn("registrationExpiring");
    });

    userAgent.on('newRTCSession', function (data) {
        console.info('onNewRTCSession: ', data);

        //通话呼入
        if (data.originator == 'remote') {
            console.info("incomingSession, answer the call----------------------");
            var accept = confirm("有来电，是否接听？");
            incomingSession = data.session;
            if (accept) {
                data.session.answer({
                    'mediaConstraints': {
                        'audio': true,
                        'video': false,
                        mandatory: {maxWidth: 640, maxHeight: 360}
                    },
                    'mediaStream': localStream
                });
            } else {
                data.session.terminate();
            }
        } else {
            console.info("outgoingSession");
            outgoingSession = data.session;
            outgoingSession.on('connecting', function (data) {
                console.info('onConnecting - ', data.request);
                currentSession = outgoingSession;
                outgoingSession = null;
            });
        }
        data.session.on('accepted', function (data) {
            console.info('onAccepted - ', data);
            if (data.originator == 'remote' && currentSession == null) {
                currentSession = incomingSession;
                incomingSession = null;
                console.info("setCurrentSession - ", currentSession);
            }
        });
        data.session.on('confirmed', function (data) {
            console.info('onConfirmed - ', data);
            if (data.originator == 'remote' && currentSession == null) {
                currentSession = incomingSession;
                incomingSession = null;
                console.info("setCurrentSession - ", currentSession);
            }
        });
        data.session.on('sdp', function (data) {
            //console.info('onSDP, type - ', data.type, ' sdp - ', data.sdp);
        });

        data.session.on('progress', function (data) {
            console.info('onProgress - ', data.originator);
            if (data.originator == 'remote') {
                console.info('onProgress, response - ', data.response);
            }
        });
        data.session.on('peerconnection', function (data) {
            console.info('onPeerconnection - ', data.peerconnection);
            data.peerconnection.onaddstream = function (ev) {
                console.info('onaddstream from remote ----------- ', ev);
                remoteVideo.srcObject = ev.stream;
                remoteVideo.play();
            };
        });
    });

    userAgent.start();

    // Register callbacks to desired call events
    var eventHandlers = {
        'progress': function (e) {
            console.log('call is in progress');
        },
        'failed': function (e) {
            console.log('call failed: ', e);
        },
        'ended': function (e) {
            console.log('call ended : ', e);
        },
        'confirmed': function (e) {
            console.log('call confirmed');
        }
    };

    function testCall() {
        var options = {
            'eventHandlers': eventHandlers,
            'mediaConstraints': {
                'audio': true, 'video': false,
                mandatory: {maxWidth: 640, maxHeight: 360}
            },
            'mediaStream': localStream
        };

        outgoingSession = userAgent.call("sip:"+$("#phone").val()+"@ssl.nicaicai.top", options);
    }
    function reg() {
        console.log('register----------->');
        userAgent.register();
    }

    function unReg() {
        console.log('unregister----------->');
        userAgent.unregister(true);
    }

    function hangup() {
        console.log('hangup----------->');
        userAgent.terminateSessions();
    }


</script>

</body>
</html>